package com.qianfeng.openapi.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianfeng.openapi.web.master.dto.TableData;
import com.qianfeng.openapi.web.master.dto.TableParam;
import com.qianfeng.openapi.web.master.entity.SysUser;
import com.qianfeng.openapi.web.master.mapper.SysUserMapper;
import com.qianfeng.openapi.web.master.service.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/25
 * @Time: 上午10:30
 */
@Service
public class SysUserServiceImpl  implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> sysUserList() {
        return sysUserMapper.selectList(null);
    }

    @Override
    public SysUser findByUsername(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);

        return sysUser;
    }

    public TableData findUserList(TableParam param){

        QueryWrapper queryWrapper = new QueryWrapper();
        if(param.getSearch()!=null&&!param.getSearch().equals("")){
            queryWrapper.like("username",param.getSearch());
            queryWrapper.or();
            queryWrapper.like("mobile",param.getSearch());
        }
        IPage<SysUser> page = new Page<>();
        page.setSize(param.getLimit());
        //int offset = (current-1)*pageSize;
        int current = param.getOffset()/param.getLimit()+1;
        page.setCurrent(current);

        page = sysUserMapper.selectPage(page,queryWrapper);

        TableData data = new TableData();
        data.setTotal(page.getTotal());
        data.setRows(page.getRecords());


        return  data;
    }


    /**
     *  [
     *                             {value: 335, name: '直接访问'},
     *                             {value: 310, name: '邮件营销'},
     *                             {value: 274, name: '联盟广告'},
     *                             {value: 235, name: '视频广告'},
     *                             {value: 400, name: '搜索引擎'}
     *                         ]
     *
     *    {"count_5000":5,"count_10000":3}
     * @return
     */
    @Override
    public List<Map<String, Object>> findPibData() {
       Map<String,Integer> map = sysUserMapper.findPieData();
       List<Map<String,Object>> list = new ArrayList<>();
      for(Map.Entry<String,Integer> entry:map.entrySet()){
          String key = entry.getKey();
          Object value = entry.getValue();
          Map<String,Object> map1 = new HashMap<>();
          map1.put("value",value);
          map1.put("name",key);

          list.add(map1);

      }

        return list;
    }

    /**
     *  xAxis: {
     *                 type: 'category',
     *                 data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
     *             },
     *
     *             series: [{
     *                 data: [120, 200, 150, 80, 70, 110, 130],
     *                 type: 'bar'
     *             }
     */
    @Override
    public Map<String, Object> findBarData() {
        Map<String, Object> stringObjectMap = new HashMap<>();
        Map<String,Integer> map = sysUserMapper.findPieData();
        List<String> xAxiosData = new ArrayList<>();
        List<Object> seriesData = new ArrayList<>();
        for(Map.Entry<String,Integer> entry:map.entrySet()){
            String key = entry.getKey();// {5000到10000:5}
            Object value = entry.getValue();

            xAxiosData.add(key);
            seriesData.add(value);
            stringObjectMap.put("xAxiosData",xAxiosData);//x坐标
            stringObjectMap.put("seriesData",seriesData);//具体的值
        }


        return stringObjectMap;
    }
}
